﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using PlanningGIS.Framework;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.Win.Controls;
////using ESRI.ArcGIS.TOCControl;


namespace PlanningGIS.TocContextMenu.Button
{
    /// <summary>
    /// 作用：移除所有图层
    /// 作者：
    /// 编写日期：2009-03-25
    /// </summary>
    internal class RemoveAllLayers : BaseCommand
    {
        IApplication m_Application = null;
        public RemoveAllLayers()
        {
            base.m_caption = "移除所有图层";
            base.m_toolTip = base.Caption;
        }

        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }

        public override void OnClick()
        {
            try
            {
                if ((m_Application as IFramework).MapControl.LayerCount == 0) return;
                //tnnd  写下面这几行bt代码 是因为ClearLayers不触发 MapControlItemDeleted事件，而在编辑模块里我要监测这个事件的
                ILayer pLayer = (m_Application as IFramework).MapControl.get_Layer(0);
                (m_Application.Document as IMxDocument).ActiveView.FocusMap.ClearLayers();
                (m_Application as IFramework).MapControl.AddLayer(pLayer, 0);

                IEngineEditor m_engineEditor = new EngineEditorClass();
                if (m_engineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
                {
                    if (m_engineEditor.HasEdits())
                    {
                        System.Windows.Forms.DialogResult dr = MsgHelper.ShowYesOrNoQueryMsgBox("是否保存编辑？");
                       if (dr == System.Windows.Forms.DialogResult.Yes)
                            m_engineEditor.StopEditing(true);
                       else if (dr == System.Windows.Forms.DialogResult.No)
                            m_engineEditor.StopEditing(false);
                    }
                    else
                    {
                        m_engineEditor.StopEditing(false);
                    }
                }
                (m_Application.Document as IMxDocument).ActiveView.FocusMap.DeleteLayer(pLayer);
                (m_Application.Document as IMxDocument).ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, (m_Application.Document as IMxDocument).ActiveView.Extent);
                (m_Application as IFramework).TOCControl.Update();
            }
            catch (System.Exception)
            {
            }
        }

        public override bool Enabled
        {
            get
            {
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }
        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.TocContextMenu.Res." + this.GetType().Name + ".ico";
                System.IO.Stream stream = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (stream == null) return 0;

                return new Bitmap(stream).GetHbitmap().ToInt32();
            }
        }
    }
}
